﻿using System;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using CryptoModule;

namespace ConsoleApplication1
{
    [Serializable]
    public class Document
    {
        private readonly string description;
        private readonly int sum;
        private readonly bool urgent;
        private readonly decimal depositNumber;

        public Document(string description, int sum, bool urgent, decimal depositNumber)
        {
            this.description = description;
            this.depositNumber = depositNumber;
            this.urgent = urgent;
            this.sum = sum;
        }

        public bool Urgent
        {
            get { return urgent; }
        }

        public decimal DepositNumber
        {
            get { return depositNumber; }
        }

        public int Sum
        {
            get { return sum; }
        }

        public string Description
        {
            get { return description; }
        }
    }

    class RSASample
    {
        static void Main()
        {
            try
            {
                var document = new Document("This is test document", 100, true, 10000203109);
                var memoryStream = new MemoryStream();
                var binaryFormatter = new BinaryFormatter();
                binaryFormatter.Serialize(memoryStream, document);
                byte[] data = memoryStream.GetBuffer();
                memoryStream.Close();
                var rsaSignature = new RSASignature();
                SortedList<string, byte[]> keys = rsaSignature.GeneraitPair();
                byte[] privateKey = keys["private"];
                byte[] publicKey = keys["public"];
                byte[] signature = rsaSignature.CreateSignature(data, privateKey);
                Console.WriteLine(rsaSignature.VerifySignature(data, signature, publicKey)
                                      ? "The data was verified."
                                      : "The data does not match the signature.");
                Console.ReadLine();
            }
            catch (ArgumentNullException)
            {
                Console.WriteLine("The data was not signed or verified");

            }
        }
    }
}